import shutil
import os
import numpy as np
import scipy.io as sio

with open('run_check_eh0_comp1_time0_girls','r',encoding='utf-8') as file:
    run_check_time0 = file.readlines()
    run_check_time0 = [s.strip() for s in run_check_time0]

with open('run_check_eh0_comp1_time1_girls','r',encoding='utf-8') as file:
    run_check_time1 = file.readlines()
    run_check_time1 = [s.strip() for s in run_check_time1]

path = './Resultados_bootstrap'

try:
    os.mkdir(path)
    print("Folder %s created!" % path)
except FileExistsError:
    print("Folder %s already exists" % path)


for i in range(1,101):
    if run_check_time0[i-1] == 'SIM':
        shutil.copy2('batch_eh0_comp1_time0_girls/eh0_comp1_time0_girls_boot'+str(i)+'/ParEst_eh0_time0_Mex_girls.mat', path +'/ParEst_eh0_time0_Mex_girls_boot'+str(i)+'.mat')
        if run_check_time1[i-1] == 'SIM':
            shutil.copy2('batch_eh0_comp1_time1_girls/eh0_comp1_time1_girls_boot'+str(i)+'/ParEst_eh0_time2_Mex_girls.mat', path +'/ParEst_eh0_time2_Mex_girls_boot'+str(i)+'.mat')
       
    
###############################################################################    
##junta todos os resultados em um arquivo .mat unico (ParEst_eh0_time0_Mex_all_boots.mat)

matrix_t0 = np.zeros((100,13))
matrix_t1 = np.zeros((100,5))

for j in range(1,101):
    if run_check_time0[j-1] == 'SIM':
        x_t0 = sio.loadmat(path + '/ParEst_eh0_time0_Mex_girls_boot'+str(j))
        x_t0 = x_t0['ParEst_eh0_time0_Mex_girls']
        matrix_t0[j-1] = x_t0
        if run_check_time1[j-1] == 'SIM':
            x_t1 = sio.loadmat(path + '/ParEst_eh0_time2_Mex_girls_boot'+str(j))
            x_t1 = x_t1['ParEst_eh0_time2_Mex_girls']
            matrix_t1[j-1] = x_t1

##As linhas referentes a pastas que nao rodaram ficam com 0
##O codigo a seguir remove as linhas com 0

matrix_t0 = np.delete(matrix_t0, np.where(matrix_t0 == np.zeros(13))[0], axis = 0)
matrix_t1 = np.delete(matrix_t1, np.where(matrix_t1 == np.zeros(5))[0], axis = 0)

###
## Adicionar coluna indicando qual o numero da pasta referente a cada resultado:


boot_num_t0 = np.zeros((100,1))
boot_num_t1 = np.zeros((100,1))

for k in range(1,101):
    if run_check_time0[k-1] == 'SIM':
        boot_num_t0[k-1] = k
    if run_check_time1[k-1] == 'SIM':
        boot_num_t1[k-1] = k

boot_num_t0 = np.delete(boot_num_t0, np.where(boot_num_t0 == 0)[0], axis = 0) ##deletando os zeros 
boot_num_t1 = np.delete(boot_num_t1, np.where(boot_num_t1 == 0)[0], axis = 0)

matrix_t0 = np.hstack((matrix_t0, boot_num_t0))
matrix_t1 = np.hstack((matrix_t1, boot_num_t1))


###############################################################################

mdic_t0 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon Jun 19 23:47:14 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh0_time0_Mex_girls_all_boots.mat': matrix_t0}
    
mdic_t1 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon Jun 19 23:47:14 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh0_time2_Mex_girls_all_boots': matrix_t1}

sio.savemat('ParEst_eh0_time0_Mex_girls_all_boots.mat', mdic_t0)
sio.savemat('ParEst_eh0_time2_Mex_girls_all_boots.mat', mdic_t1)